<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2018/12/29
 * Time: 14:01
 * 待机广告
 */
namespace app\admin\controller;
use think\Db;
class Standby extends Base {
        public function index(){
            if(request()->isAjax ()){
                extract(input());
                $map['b_user'] = 0;
                $page = input('page') ? input('page'):1;
                $limit = input("limit")?input("limit"):10;// 获取总条数;
                $count = Db::table('up_banner_hold')->alias('a')
                    ->where($map)
                    ->count();//计算总页面
                $list = Db::table('up_banner_hold')
                    ->alias ('a')
                    ->page($page, $limit)
                    ->where($map)
                    ->select();
                foreach ($list as $key=>&$vo){
                    if($vo['b_user']!=0){
                        $p_username = Db::table('up_province')->where('p_id',$vo['b_user'])->value('p_username');
                        $vo['b_user'] =$p_username;
                    }else{
                        $vo['b_user'] ="超级管理员";
                    }
                }
                return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$list]);
            }
            return $this->fetch();
        }

    /*****
     * @return \think\response\Json
     * 删除
     */
    public function del(){
        $id = input('param.id');
        $res=  Db::table('up_banner_hold')->where('b_id',$id)->delete();
        if($res){
            return json(['code' => 200, 'data' => '', 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'data' => '', 'msg' => '删除失败']);
        }
    }

    /**
     * @return mixed
     * 编辑
     */
    public function update(){
        if(request()->isPost()){
            $id = input('param.id');
            $data['b_name'] = input('param.b_name');
            $data['b_type'] = input('param.b_type');
            $data['b_url'] = input('param.img_file');
            if(input('param.b_type')==3){
                $data['b_mp_url'] = input('param.lay-img');
            }
            $data['b_update_time']=date('Y-m-d H:i:s',time());
            $data['b_user'] = 0;
            $map['b_id'] = $id;
            $aa = Db::table('up_banner_hold')->where($map)->update($data);
            if($aa){
                return json(['code' => 200, 'data' =>"", 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'data' => '', 'msg' => '编辑失败']);
            }
        }
        $id = input('param.id');
        $res = Db::table('up_banner_hold')->where('b_id',$id)->find();
        $this->assign('res',$res);
        return $this->fetch();
    }
}